package com.lanou3g.dllo.baidumusic.musiclibrary.ranklist.ranklistdetail;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou3g.dllo.baidumusic.R;
import com.lanou3g.dllo.baidumusic.tools.MyRvOnClickListener;
import com.lanou3g.dllo.baidumusic.tools.OnRemoveListener;
import com.lanou3g.dllo.baidumusic.tools.SingleVolley;

import java.util.List;

/**
 * Created by dllo on 16/6/22.
 */
public class RanklistDetailAdapter extends RecyclerView.Adapter<RanklistDetailAdapter.MyViewHolder>{

    private Context context;
    private RanklistDetailBean ranklistDetailBean;
    private List<RanklistDetailBean.SongListBean> list;
    private MyRvOnClickListener myRvOnClickListener;
    private OnRemoveListener onRemoveListener;

    public void setOnRemoveListener(OnRemoveListener onRemoveListener) {
        this.onRemoveListener = onRemoveListener;
    }

    public void setMyRvOnClickListener(MyRvOnClickListener myRvOnClickListener) {
        this.myRvOnClickListener = myRvOnClickListener;
    }

    public RanklistDetailAdapter(Context context) {
        this.context = context;
    }

    public void setRanklistDetailBean(RanklistDetailBean ranklistDetailBean) {
        this.ranklistDetailBean = ranklistDetailBean;
        list = ranklistDetailBean.getSong_list();
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.item_ranklistdetail,parent,false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {

        holder.ranklistdetailSongsnameTv.setText(list.get(position).getTitle());
        holder.ranklistdetailSingerTv.setText(list.get(position).getAuthor());
        SingleVolley.getSingleVolley(context).getImageLoader().get(list.get(position).getPic_big(),
                ImageLoader.getImageListener(holder.ranklistdetailHeadphotoIv,R.mipmap.ic_launcher,R.mipmap.ic_launcher));
        holder.ranklistLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 当itemView被点击的时候
                // getLayoutPosition是用来获得当前点击的position
                int position = holder.getLayoutPosition();
                // 调用接口对象的onClick方法
                myRvOnClickListener.onClick(position);
            }
        });

        holder.ranklistDetailMoreBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRemoveListener.onRemoveClick(position);
            }
        });
    }


    @Override
    public int getItemCount() {
        return list == null ? 0: list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{

        ImageView ranklistdetailHeadphotoIv;
        TextView ranklistdetailSongsnameTv,ranklistdetailSingerTv;
        LinearLayout ranklistLinearLayout;
        ImageView ranklistDetailMoreBtn;

        public MyViewHolder(View itemView) {
            super(itemView);

            ranklistdetailHeadphotoIv = (ImageView) itemView.findViewById(R.id.ranklistdetail_headphoto_iv);
            ranklistdetailSongsnameTv = (TextView) itemView.findViewById(R.id.ranklistdetail_songsname_tv);
            ranklistdetailSingerTv = (TextView) itemView.findViewById(R.id.ranklistdetail_singer_tv);
            ranklistLinearLayout = (LinearLayout) itemView.findViewById(R.id.ranklist_linearlayout);
            ranklistDetailMoreBtn = (ImageView) itemView.findViewById(R.id.ranklistdetail_more_btn);
        }
    }
}
